package com.tianqu.tms.www.transfer.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferPersonalCarInfo;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgAdvanceInputViewHelper;

public class TransferRgAdvanceSearchResultDispatchController extends TmsController {
	private Logger logger = Logger.getLogger(TransferRgAdvanceSearchResultDispatchController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = getTxController();
		try{
			controller.begin();
			TransferRgAdvanceInputViewHelper view = (TransferRgAdvanceInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
			TransferPersonalCarInfo person = new TransferPersonalCarInfo();
			String basicCarId = view.getBasicCarId();
			String serial = view.getSerial();
			TmsUserProfile profile = uc.getUserProfile();
			person = person.getPersonalCarInfoBySerial(basicCarId);
			Car car =new Car();
			TransferAcceptedInfo accept = new TransferAcceptedInfo();
			accept = accept.getAcceptInfoBySerial(serial,"");
			if(accept!=null)
			{
					car = car.searchAllCarByLicensePlate(person.getLicensePlate());
					if(car!=null)
					{
//						if(car.getRegisterStatus() == 1 || car.getRegisterStatus() == 2)
						if(car.getRegisterStatus() != 3 && car.getRegisterStatus() != 4)//modify by huangwei
						{
							person.deletePersonalInfoBySerial(basicCarId);//删除	
							car.setStatus(3);
							car.setRegisterStatus(car.getRegisterStatus());
							car.setUpdateUserId(profile.getUniqueId());
							car.updateCarStatus(car);//更新
							
							//过户表更新
							accept.setMarketType(1);
							accept.setBasicCarId(car.getSerial());
							accept.setSerial(serial);
							accept.setUpdateUserId(profile.getUniqueId());
							//更新与库存表关联信息
							accept.setLicensePlate(car.getLicensePlate());
							accept.setCarOwner(car.getCarOwner());
							accept.setCarOwnerIdCardNo(car.getCarOwnerIdCardNo());
							accept.setOwnerAddress(car.getOwnerAddress());
							accept.setOwnerOrgNo(car.getOwnerOrgNo());
							accept.setOwnerPhone(car.getOwnerPhone());
							accept.setOwnerType(car.getOwnerType());
							accept.setPrice(car.getPrice());
							accept.setVin(car.getVin());
							
							accept.updateMarketTypeBySerial(accept);					
						}else{
							car = setCarFromEntity(car, person);
							String newSerial = basicCarId;
							car.setSerial(newSerial);
							car.setInsertUserId(profile.getUniqueId());
							car.setUpdateUserId(profile.getUniqueId());
							car.setStatus(3);
							car.setAttestStatus(1);
							car.setRegisterStatus(2);
							car.setEntryStatus(4);
							car.setCarScore(7);// 楼层
							car.setMemberId(Integer.parseInt(view.getMemberId()));
							car.insertPersonalCarInfo(car);

							// 更新过户信息表
							accept.setMarketType(1);
							accept.setBasicCarId(newSerial);
							accept.setSerial(serial);
							accept.setUpdateUserId(profile.getUniqueId());
							
							accept.setLicensePlate(person.getLicensePlate());
							accept.setCarOwner(person.getCarOwner());
							accept.setCarOwnerIdCardNo(person.getCarOwnerIdCardNo());
							accept.setOwnerAddress(person.getOwnerAddress());
							accept.setOwnerOrgNo(person.getOwnerOrgNo());
							accept.setOwnerPhone(person.getOwnerPhone());
							accept.setOwnerType(person.getOwnerType());
							accept.setPrice(person.getPrice());
							accept.setVin(person.getVin());
							
							accept.updateMarketTypeBySerial(accept);
							person.deletePersonalInfoBySerial(basicCarId);						
						}
					}else{
						car = setCarFromEntity(car, person);
						String newSerial = basicCarId;
						car.setSerial(newSerial);
						car.setInsertUserId(profile.getUniqueId());
						car.setUpdateUserId(profile.getUniqueId());
						car.setStatus(3);
						car.setAttestStatus(1);
						car.setRegisterStatus(2);
						car.setEntryStatus(4);
						car.setCarScore(7);// 楼层
						car.setMemberId(Integer.parseInt(view.getMemberId()));
						car.insertPersonalCarInfo(car);

						// 更新过户信息表
						accept.setMarketType(1);
						accept.setBasicCarId(newSerial);
						accept.setSerial(serial);
						accept.setUpdateUserId(profile.getUniqueId());
						
						accept.setLicensePlate(person.getLicensePlate());
						accept.setCarOwner(person.getCarOwner());
						accept.setCarOwnerIdCardNo(person.getCarOwnerIdCardNo());
						accept.setOwnerAddress(person.getOwnerAddress());
						accept.setOwnerOrgNo(person.getOwnerOrgNo());
						accept.setOwnerPhone(person.getOwnerPhone());
						accept.setOwnerType(person.getOwnerType());
						accept.setPrice(person.getPrice());
						accept.setVin(person.getVin());
						
						accept.updateMarketTypeBySerial(accept);
						person.deletePersonalInfoBySerial(basicCarId);	
					}
				}
			controller.commit();
		}catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");			
	}

	private Car setCarFromEntity(Car car, TransferPersonalCarInfo person) {
		car = new Car();
		car.setAttribute(person.getAttribute());
		car.setBrandId(person.getBrandId());
		car.setCardType(person.getCardType());
		car.setLicensePlate(person.getLicensePlate());
		car.setCarOwner(person.getCarOwner());
		car.setCarOwnerIdCardNo(person.getCarOwnerIdCardNo());
		car.setCarType(person.getCarType());
		car.setCarUse(person.getCarUse());
		car.setColor(person.getColor());
		car.setCompanyContactPerson(person.getCompanyContactPerson());
		car.setCompanyContactPhone(person.getCompanyContactPhone());
		car.setCountryStyle(person.getCountryStyle());
		car.setDisplacement(person.getDisplacement());
		car.setEngine(person.getEngine());
		car.setFuelType(person.getFuelType());
		car.setGearbox(person.getGearbox());
		car.setJqx(person.getJqx());
		car.setLoadquality(person.getLoadquality());
		car.setCarStyleId(person.getCarStyleId());
		car.setMileage(person.getMileage());
		car.setModelCode(person.getModelCode());
		car.setOwnerAddress(person.getOwnerAddress());
		car.setOwnerOrgNo(person.getOwnerOrgNo());
		car.setOwnerPhone(person.getOwnerPhone());
		car.setOwnerType(person.getOwnerType());
		car.setPassenger(person.getPassenger());
		car.setPrice(person.getPrice());
		car.setCarName(person.getCarName());
		car.setVendorId(person.getVendorId());
		car.setRegDate(person.getRegDate());
		car.setRegisterCardNo(person.getRegisterCardNo());
		car.setSendCardDate(person.getSendCardDate());
		car.setSerial(person.getSerial());
		car.setPhoto_1(person.getPhoto_1());
		car.setPhoto_2(person.getPhoto_2());
		car.setPhoto_3(person.getPhoto_3());
		car.setPhoto_4(person.getPhoto_4());
		car.setPhoto_5(person.getPhoto_5());
		car.setPhoto_6(person.getPhoto_6());
		car.setPhoto_7(person.getPhoto_7());
		car.setPhoto_8(person.getPhoto_8());
		car.setVendorCode(person.getVendorCode());
		if(person.getVin()!=null)
		{
			car.setVin(person.getVin());
		}
		car.setYearCarefulPeriod(person.getYearCarefulPeriod());
		return car;
	}
}
